package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-ease/cache"
	"order-ease/model"
	"strconv"
	"time"
)

func GoodCreate(c *gin.Context) {
	goodname := c.PostForm("goodname")
	goodno := c.PostForm("goodno")
	goodher := c.PostForm("goodher")
	goodmer := c.PostForm("goodmer")

	data := model.Good{
		GoodName: goodname,
		GoodNo:   goodno,
		GoodHer:  goodher,
		GoodMer:  goodmer,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	if data.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrdersCreate(c *gin.Context) {
	ordersname := c.PostForm("ordersname")
	orderscount := c.PostForm("orderscount")
	ordersprice := c.PostForm("ordersprice")
	gid, _ := strconv.Atoi(c.PostForm("gid"))

	data := model.Orders{
		OrdersName:  ordersname,
		OrdersCount: orderscount,
		OrdersPrice: ordersprice,
		Gid:         gid,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	if data.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	//SELECT
	//goods.good_name AS GoodName,
	//	goods.good_no AS GoodNo,
	//	goods.good_her AS GoodHer,
	//	goods.good_mer AS GoodMer,
	//	orders.orders_name AS OrdersName,
	//	orders.orders_count AS OrdersCount,
	//	orders.orders_price AS OrdersPrice
	//FROM
	//orders
	//
	//LEFT JOIN goods ON orders.gid = goods.id
	var article []model.GoodOrders
	redis_key := "list_" + strconv.Itoa(page)
	createData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if createData == "" {

		model.DB.Table("orders").
			Select(
				"goods.good_name AS GoodName",
				"goods.good_no AS GoodNo",
				"goods.good_her AS GoodHer",
				"goods.good_mer AS GoodMer",
				"orders.orders_name AS OrdersName",
				"orders.orders_count AS OrdersCount",
				"orders.orders_price AS OrdersPrice",
			).Joins("LEFT JOIN goods ON orders.gid = goods.id").
			Offset(offset).Limit(size).Find(&article)
		jsonData, _ := json.Marshal(article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(createData), &article)
	}
	var total int64
	model.DB.Model(model.Orders{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  article,
		"total": total,
	})
	return

}
func OrdersList(c *gin.Context) {
	id := c.Query("id")

	var article model.GoodOrders
	redis_key := "orders_list" + id
	createData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if createData == "" {
		model.DB.Table("orders").
			Select(
				"goods.good_name AS GoodName",
				"goods.good_no AS GoodNo",
				"goods.good_her AS GoodHer",
				"goods.good_mer AS GoodMer",
				"orders.orders_name AS OrdersName",
				"orders.orders_count AS OrdersCount",
				"orders.orders_price AS OrdersPrice",
			).Joins("LEFT JOIN goods ON orders.gid = goods.id").
			Find(&article)
		jsonData, _ := json.Marshal(article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(createData), &article)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": article,
	})
	return
}
